package com.gree.auth.util;

import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

public class DecryptUtil {

    private static final String MAC_NAME = "HmacSHA1";
    private static final String ENCODING = "UTF-8";
    private static final String PKCS7_NAME = "AES/CBC/PKCS7Padding";

    public static String getHmacSign(String content, String charset,String key,String hamaAlgorithm){
        byte[] result = null;
        try {
            //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
            SecretKeySpec signinKey = new SecretKeySpec(key.getBytes(), hamaAlgorithm);
            //生成一个指定 Mac 算法 的 Mac 对象
            Mac mac = Mac.getInstance(hamaAlgorithm);
            //用给定密钥初始化 Mac 对象
            mac.init(signinKey);
            //完成 Mac 操作
            byte[] rawHmac;
            rawHmac = mac.doFinal(content.getBytes(charset));
            result = Base64.encodeBase64(rawHmac);

        } catch (NoSuchAlgorithmException e) {
            System.err.println(e.getMessage());
        } catch (InvalidKeyException e) {
            System.err.println(e.getMessage());
        }  catch (IllegalStateException | UnsupportedEncodingException e) {
            System.err.println(e.getMessage());
        }
        if (null != result) {
            return new String(result);
        } else {
            return null;
        }
    }

    private static AlgorithmParameters generateIV(String ivVal) throws Exception{
        byte[]iv=ivVal.getBytes("UTF-8");
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        return params;
    }

    public static String decryptbyaes(String encryptedStr,String aesKey,String ivVal) throws Exception{
        byte[] encryptedData=Base64.decodeBase64(encryptedStr);
        byte[] keyBytes = aesKey.getBytes("UTF-8");
        SecretKeySpec key = new SecretKeySpec(keyBytes,"AES");
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        Cipher cipher = Cipher.getInstance(PKCS7_NAME);
        AlgorithmParameters iv=generateIV(ivVal);
        //设置为解密模式
        cipher.init(Cipher.DECRYPT_MODE, key,iv);
        byte[] decryptData=cipher.doFinal(encryptedData);
        return new String(decryptData,"UTF-8");
    }


    public static String encryptbyaes(String plainText,String aesKey,String ivVal) throws Exception {
        byte[] data=plainText.getBytes("UTF-8");
        AlgorithmParameters iv=generateIV(ivVal);
        byte[] keyBytes = aesKey.getBytes();
        SecretKeySpec key = new SecretKeySpec(keyBytes,"AES");
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key,iv);
        byte[] encryptData= cipher.doFinal(data);
        return Base64.encodeBase64String(encryptData);
    }
}